package pers.xhh.shopapi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.xhh.shopapi.annotation.UserLoginToken;
import pers.xhh.shopapi.common.Mi;
import pers.xhh.shopapi.entity.to.category.MiCategoryNameTo;
import pers.xhh.shopapi.entity.to.category.MiCategoryTo;
import pers.xhh.shopapi.service.MiCategoryService;

import javax.validation.Valid;

/**
 * @author wei-xhh
 * @date 2020-11-24
 */
@CrossOrigin
@RestController
@RequestMapping("/api/v1")
public class MiCategoryController {

    @Autowired
    MiCategoryService miCategoryService;

    @UserLoginToken
    @GetMapping("/categories")
    public Mi updateRights(@RequestParam(required = false) Integer type,
                           @RequestParam(required = false) Integer pagenum,
                           @RequestParam(required = false) Integer pagesize){
        return miCategoryService.listCategory(type, pagenum, pagesize);
    }

    @UserLoginToken
    @PostMapping("/categories")
    public Mi insertCat(@Valid @RequestBody MiCategoryTo miCategoryTo){
        return miCategoryService.saveCategory(miCategoryTo);
    }

    @UserLoginToken
    @GetMapping("/categories/{id}")
    public Mi getCategoryById(@PathVariable Integer id){
        return miCategoryService.getCategoryById(id);
    }

    @UserLoginToken
    @PutMapping("/categories/{id}")
    public Mi updateCatName(@PathVariable Integer id,
                            @Valid @RequestBody MiCategoryNameTo miCategoryNameTo){
        return miCategoryService.updateCatName(id, miCategoryNameTo);
    }

    @UserLoginToken
    @DeleteMapping("/categories/{id}")
    public Mi deleteCat(@PathVariable Integer id){
        return miCategoryService.deleteCat(id);
    }

    @UserLoginToken
    @GetMapping("/cateOneList")
    public Mi getCateOneList(){
        return miCategoryService.getCateOneList();
    }

}
